package ReportHistogram;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.BoxLayout;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JButton;

/**
 * show the dynamic histogram
 * 
 * @author omri&gilad&amit
 * 
 */
public class ReportHistogram extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3049995314469783566L;
	private JTable table; // makes histogram out of this table
	public int values[]; // values to display as histogram
	private String[] columnHeader; // names for columns
	public Color[][] cellColors; // color of each cell in table
	public int histogramHeight; // number of rows
	private int maxColumnWidth; // column width
	private JPanel southPanel; // contains the table header (column names)
	private final int padding; // if true - expends the histogram to fit view
								// port width
	private int lastColor = -1; // flag for last color used to paint a column
	private int secondLastColor = -1; // flag for second last color used to
										// paint column
	private Random rand; // random number generator

	/**
	 * constructs a histogram with given values and column names
	 * 
	 * @param columnTitle
	 *            An Array of type String representing column headers
	 * @param colunmvalues
	 *            An Array of type integer representing values for the histogram
	 * @param hstgrmHgt
	 *            Number of Rows in the histogram
	 * @param ColumnWidth
	 *            columns width
	 * @param fitViewPort
	 *            Boolean parameter indicating if the histogram should fill View
	 *            port space
	 * @throws Exception
	 */
	public ReportHistogram(String[] columnTitle, int[] colunmvalues,
			int hstgrmHgt, int ColumnWidth) 
			throws Exception {
		super();
		int i, j;
		rand = new Random();

		/* input checks */
		if (colunmvalues == null) {
			throw new Exception("values array must be initialized");
		}
		if (hstgrmHgt <= 1) {
			throw new Exception("Histogram Height must be larger than 1");
		}
		if (ColumnWidth <= 0) {
			throw new Exception("Histogram width must be larger than 1");
		}
		if (columnTitle.length != colunmvalues.length)
			throw new Exception(
					"must have an equeale number of column names and values");

		setLayout(new BorderLayout(0, 0));
	
			padding = 1;
	
		
			values = new int[2  + colunmvalues.length];
			columnHeader = new String[2  + colunmvalues.length];
		

		// sets values to arrays
		for (i = 0; i < columnHeader.length; i++) {
			if ((i < 1) || (i > columnHeader.length - 2 )) {
				this.columnHeader[i] = "";
				this.values[i] = -1;
			} else {
				this.columnHeader[i] = columnTitle[i - 1];
				this.values[i] = colunmvalues[i - 1];
			}
		}

		this.histogramHeight = hstgrmHgt;
		this.maxColumnWidth = ColumnWidth;

		/* column colors */
		Color[] colorPallet = { new Color(215, 255, 153),
				new Color(240, 240, 132), new Color(149, 175, 215),
				new Color(217, 150, 148), new Color(191, 191, 191),
				new Color(195, 214, 155), new Color(178, 162, 199),
				new Color(146, 205, 220), new Color(250, 192, 143),
				new Color(220, 152, 210) };

		table = new JTable();
		table.setEnabled(false);
		table.setShowVerticalLines(false);
		table.setRowSelectionAllowed(false);
		table.setFillsViewportHeight(true);
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(true);

		Object obj[][] = new Object[histogramHeight][values.length];
		for (int i1 = 0; i1 < histogramHeight; i1++)
			for (int j1 = 0; j1 < values.length; j1++)
				obj[i1][j1] = null;// initialize table model matrix

		table.setModel(new DefaultTableModel(obj, columnHeader));

		/*-----  setting cell colors -----*/
		cellColors = new Color[this.values.length][histogramHeight];

		for (i = 0; i < this.values.length; i++) {
			boolean flag = true;

			int tempColor = Math.abs((rand.nextInt() % 10));
			while ((tempColor == lastColor) || (tempColor == secondLastColor))
				tempColor = Math.abs((rand.nextInt() % 10));

			secondLastColor = lastColor;
			lastColor = tempColor;

			for (j = 0; j < histogramHeight; j++) { // number of rows is dynamic
				float val = values[i];
				val = val / 100;
				val = val * histogramHeight;
				if (val >= (float) j) {
					cellColors[i][table.getRowCount() - j - 1] = colorPallet[tempColor];
					if (j == histogramHeight - 1)
						table.getModel().setValueAt(
								Integer.toString(values[i]), 0, i);
				} else {
					if ((flag) && (values[i] != -1)) {
						table.getModel().setValueAt(
								Integer.toString(values[i]),
								Math.max(0, histogramHeight - j), i);
						flag = false;
					}
					cellColors[i][table.getRowCount() - j - 1] = Color.WHITE;
				}
			}
		}

		TableColumn tcol;// set column model
		for (i = 1; i < table.getColumnCount() - 1; i++) {
			tcol = table.getColumnModel().getColumn(i);
			tcol.setCellRenderer(new MyTableCellRenderer(this.cellColors));
			tcol.setMaxWidth(maxColumnWidth);
		}

		southPanel = new JPanel();// add column header to south panel
		southPanel.setBackground(Color.WHITE);
		southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.PAGE_AXIS));
		add(southPanel, BorderLayout.SOUTH);

		southPanel.add(table);
		southPanel.add(table.getTableHeader());

	}
}

/**
 * private class which extends the defaultTableCellRenderer used to render table
 * cells in different colors.
 * 
 */
class MyTableCellRenderer extends DefaultTableCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2520714867209949742L;

	private Color colors[][];// matrix of colors the size of the table

	public MyTableCellRenderer(Color[][] cellColors) {
		super();
		this.colors = cellColors;
	}

	public Component getTableCellRendererComponent(JTable table, Object obj,
			boolean isSelected, boolean hasFocus, int row, int column) {

		Component cell = super.getTableCellRendererComponent(table, obj, false,
				false, row, column);

		cell.setBackground(colors[column][row]);
		setHorizontalAlignment(SwingConstants.CENTER);
		return cell;
	}

}// end MyTableCellRenderer

